package com.personalblog.controller.admin;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.personalblog.mapper.TypeMapper;
import com.personalblog.po.Tag;
import com.personalblog.po.Type;
import com.personalblog.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.List;

/**
 * @author jxau_scf
 * @date 2021/3/23 - 21:25
 */
@Controller
@RequestMapping("/admin")
public class TypeController {
    @Autowired
    private TypeService typeService;

    @GetMapping("/types")
    public String types(@RequestParam(defaultValue = "1") int page, Model model){
        //查询之前传入当前页，和每页记录数
        PageHelper.startPage(page,5);
        List<Type> typeList = typeService.findTypes();
        PageInfo pageInfo = new PageInfo(typeList);
        model.addAttribute("pageInfo",pageInfo);
        return "admin/types";
    }

    @GetMapping("/types/input")
    public String typeInput(Model model){
        model.addAttribute("type",new Type());
        return "admin/types-input";
    }

    @GetMapping("/types/{id}/input")
    public String editInput(@PathVariable int id, Model model){
        model.addAttribute("type", typeService.findTypeById(id));
        return "admin/types-input";
    }

    @PostMapping("/types")
    public String post(Type type, BindingResult bindingResult, RedirectAttributes attributes){
        Type type1 = typeService.findTypeByName(type.getTypeName());
        if (type1 != null){
            bindingResult.rejectValue("name","nameError","不能重复添加标签");
        }
        if(bindingResult.hasErrors()){
            return "admin/types-input";
        }
        int i = typeService.insertType(type);
        if (i == 0){
            attributes.addFlashAttribute("message","新增失败");
        }else {
            attributes.addFlashAttribute("message","新增成功");
        }
        return "redirect:/admin/types";
    }

    @PostMapping("/types/{id}")
    public String editPost(Type type, @PathVariable int id, BindingResult bindingResult, RedirectAttributes attributes){
        Type type1 = typeService.findTypeByName(type.getTypeName());
        if (type1 != null){
            bindingResult.rejectValue("name","nameError","不能重复添加标签");
        }
        if(bindingResult.hasErrors()){
            return "admin/types-input";
        }
        int i = typeService.updateType(id,type);
        if (i == 0){
            attributes.addFlashAttribute("message","更新失败");
        }else {
            attributes.addFlashAttribute("message","更新成功");
        }
        return "redirect:/admin/types";
    }

    @GetMapping("/types/{id}/delete")
    public String deleteType(@PathVariable int id, RedirectAttributes attributes){
        int i = typeService.deleteType(id);
        if (i == 0){
            attributes.addFlashAttribute("message","删除失败");
        }else {
            attributes.addFlashAttribute("message","删除成功");
        }
        return "admin/types";
    }
}
